package com.daka.service.impl;

import com.daka.dao.VolunteerInsuranceDao;
import com.daka.dao1.MemberInsuranceDao;
import com.daka.entity.MemberInsurance;
import com.daka.entity.User;
import com.daka.entity.VolunteerInsurance;
import com.daka.service.VolunteerInsuranceService;
import com.daka.tools.ConfigUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * Date: 2016/7/25 0025
 * Time: 11:24
 */
@Service("volunteerInsuranceService")
public class VolunteerInsuranceServiceImpl implements VolunteerInsuranceService {

    @Resource
    private MemberInsuranceDao memberInsuranceDao;

    @Resource
    private VolunteerInsuranceDao volunteerInsuranceDao;

    public void migrate(Integer max,Integer min) {

        List<MemberInsurance> memberInsuranceList;
        if(max == 0 || min == 0){
            memberInsuranceList = memberInsuranceDao.selectAll();
        }else{
            memberInsuranceList = memberInsuranceDao.selectByIdBetween(max, min);
        }

        List<VolunteerInsurance> volunteerInsuranceList = new ArrayList<VolunteerInsurance>();

        for (MemberInsurance memberInsurance : memberInsuranceList) {

            volunteerInsuranceList.add(VolunteerInsurance.build(memberInsurance));

            if (volunteerInsuranceList.size() >= ConfigUtil.BATCH_INSERT_MAX) {
                int i = volunteerInsuranceDao.insertBatch(volunteerInsuranceList);

                if (i > 0) {
                    volunteerInsuranceList.clear();
                }
            }

        }

        if (volunteerInsuranceList.size() > 0) {
            volunteerInsuranceDao.insertBatch(volunteerInsuranceList);
        }
    }
}
